from django.http import JsonResponse, HttpResponse
from lib.common import AbstractHandler
from apps.student.share import app_logger
from services.asyntask.idle_tasks import statisticsUser
from lib.common import CacheKeys
from django.core.cache import cache
# from services.asyntask.idle_tasks import statisticsAgentdatas
from services.datamodel.student.models import mm_Student
import datetime
from services.datamodel.utils import Utils


class Handler(AbstractHandler):
    def __init__(self):
        AbstractHandler.__init__(self, app_logger)
        self.app_logger = app_logger
        self.METHOD_TAB = {
            'POST': {
            },
            'GET': {
                'index': self.index,
                'statistical_data': self.statistical_data,
                'headmaster_statistical_data': self.headmaster_statistical_data,
                'export_statistical_data': self.export_statistical_data
            }
        }

    def index(self, request):
        total_user_key = CacheKeys.TOTAL_USER
        total_user = cache.get(total_user_key)  # 总的用户数
        member_vip_key = CacheKeys.MEMBER_VIP
        member_vip = cache.get(member_vip_key)  # 会员用户
        sheng_agent_key = CacheKeys.SHENG_AGENT
        sheng_agent = cache.get(sheng_agent_key)  # 省代理
        shi_agent_key = CacheKeys.SHI_AGENT
        shi_agent = cache.get(shi_agent_key)  # 市代理
        qu_agent_key = CacheKeys.QU_AGENT
        qu_agent = cache.get(qu_agent_key)  # 市代理
        supplier_key = CacheKeys.SUPPLIER
        supplier = cache.get(supplier_key)  # 供应商
        platinum_key = CacheKeys.PLATINUM
        platinum = cache.get(platinum_key)  # 供应商
        spokesman_key = CacheKeys.SPOKESMAN
        spokesman = cache.get(spokesman_key)  # 供应商
        platform_total_cards_key = CacheKeys.PLATFORM_TOTAL_CARDS
        platform_total_cards = cache.get(platform_total_cards_key)  # 平台总卖出卡数
        c_activation_key = CacheKeys.C_ACTIVATION
        c_activation = cache.get(c_activation_key)  # 平台总卖出卡数
        datas = {
            'total_user': total_user,
            'member_vip': member_vip,
            'sheng_agent': sheng_agent,
            'shi_agent': shi_agent,
            'qu_agent': qu_agent,
            'supplier': supplier,
            'platinum': platinum,
            'spokesman': spokesman,
            'platform_total_cards': platform_total_cards,
            'c_activation': c_activation
        }
        return self.success(datas)

    def statistical_data(self, request):
        """
        针对投放数据的统计
        :param request:
        :return:
        """
        zero_month, last_month = Utils.getMonthDatetime()
        start_date = request.GET.get('start_date', zero_month)
        end_date = request.GET.get('end_date', last_month)
        headmaster_id = request.GET.get('headmaster_id', 0)
        return self.success(mm_Student.statistical_data(start_date=start_date, end_date=end_date,
                                                        headmaster_id=headmaster_id))

    def headmaster_statistical_data(self, request):
        """
        针对总数据班主任数据
        :param request:
        :return:
        """
        zero_month, last_month = Utils.getMonthDatetime()
        start_date = request.GET.get('start_date', zero_month)
        end_date = request.GET.get('end_date', last_month)
        headmaster_id = request.GET.get('headmaster_id', 0)
        page = request.GET.get('page', 0)
        export = request.GET.get('export', 0)
        if export:
            return mm_Student.headmaster_statistical_data(page=page, start_date=start_date, end_date=end_date,
                                                          headmaster_id=headmaster_id, export=export)
        return self.success(mm_Student.headmaster_statistical_data(page=page, start_date=start_date, end_date=end_date,
                                                                   headmaster_id=headmaster_id))

    def export_statistical_data(self, request):
        zero_month, last_month = Utils.getMonthDatetime()
        start_date = request.GET.get('start_date', zero_month)
        end_date = request.GET.get('end_date', last_month)
        headmaster_id = request.GET.get('headmaster_id', 0)
        return mm_Student.export_statistical_data(start_date=start_date, end_date=end_date, headmaster_id=headmaster_id)

handler = Handler()
